package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.mapper.EmployeeMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IEmployeeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EmployeeServiceImpl implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public void delete(Long id) {
        employeeMapper.deleteRelation(id);
        employeeMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void save(Employee employee, Long[] roleIds) {
        employeeMapper.insert(employee);
        if (roleIds != null && roleIds.length > 0) {
            for (Long roleId : roleIds) {
                employeeMapper.insertRelation(employee.getId(), roleId);
            }
        }

    }

    @Override
    public Employee get(Long id) {
        return employeeMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Employee> listAll() {
        return employeeMapper.selectAll();
    }

    @Override
    public void update(Employee employee, Long[] roleIds) {
        employeeMapper.updateByPrimaryKey(employee);
        employeeMapper.deleteRelation(employee.getId());
        if (roleIds != null && roleIds.length > 0) {
            for (Long roleId : roleIds) {
                employeeMapper.insertRelation(employee.getId(), roleId);
            }
        }

    }

    @Override
    public PageInfo<Employee> query(QueryObject qo) {

        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        return new PageInfo<>(employeeMapper.selectForList(qo));
    }

    @Override
    public Boolean checkUserName(String username) {
        Employee employee = employeeMapper.selectByUserName(username);
        return employee == null;
//        return employee == null;
    }

    @Override
    public Employee login(String username, String password) {
        Employee employee = employeeMapper.selectByUsernameAndPassword(username, password);
        return employee;
    }

    @Override
    public Workbook export() {
        List<Employee> employees = this.listAll();
        Workbook wb = new HSSFWorkbook();
        Sheet sheet = wb.createSheet("员工");
        Row headRow = sheet.createRow(0);
        headRow.createCell(0).setCellValue("用户名");
        headRow.createCell(1).setCellValue("姓名");
        headRow.createCell(2).setCellValue("邮箱");
        headRow.createCell(3).setCellValue("年龄");

        for (int i = 0; i < employees.size(); i++) {
            Row row = sheet.createRow(i + 1);
            Employee employee = employees.get(i);
            row.createCell(0).setCellValue(employee.getUsername());
            row.createCell(1).setCellValue(employee.getName());
            row.createCell(2).setCellValue(employee.getEmail());
            row.createCell(3).setCellValue(employee.getAge());
        }
        return wb;
    }
        //使用POI API读取excel文件内容
    @Override
    public void improtXls(Workbook wb) {
        Sheet sheet = wb.getSheetAt(0);
        // 获取最后一行的索引
        int lastRowNum = sheet.getLastRowNum();
        // 从索引为 1 的行数开始读（忽略标题行）
        for (int i = 1; i <= lastRowNum; i++) {
            Employee employee = new Employee();
            // 获取行数据，封装到员工对象的对应属性上
            Row row = sheet.getRow(i);
            employee.setName(row.getCell(0).getStringCellValue());
            employee.setUsername(row.getCell(1).getStringCellValue());
            employee.setEmail(row.getCell(2).getStringCellValue());
            employee.setAge((int) row.getCell(3).getNumericCellValue());
            employee.setPassword("1");
            // 调用保存方法
            employeeMapper.insert(employee);
        }

    }
}
